<template>
  <Header class="headerbox" :navShow="true" />
  <div class="login_wrapper">
    <div class="contidnex"></div>
    <div class="login_content">
      <div class="login_left">
        <img
          src="@/assets/logo-h@2x.png"
          alt=""
          class="logo_img"
        />
        <p class="text1">
          Just one step <br />
          Start your smart investing
        </p>
        <p class="text2">
          The stock market is <br />
          The best way to invest your money
        </p>
      </div>
      <div class="login_right">
        <div class="right_content">
          <p class="page_titlte">Register</p>
          <div class="input_content">
            <div class="login_input">
              <i class="iconfont icon-wodedangxuan"></i>
              <input
                type="text"
                placeholder="请输入用户名"
                class="input_text"
                v-model="regiserData.username"
              />
            </div>
            <div class="login_input">
              <i class="iconfont icon-xinfeng"></i>
              <input
                type="text"
                placeholder="请输入邮箱"
                class="input_text"
                v-model="regiserData.email"
              />
            </div>
            <div class="login_input">
              <i class="iconfont icon-caidan-anquan1x"></i>
              <div class="input_right">
                <input
                  type="text"
                  placeholder="请输入验证码"
                  class="input_text"
                  v-model="regiserData.verify_code"
                />
                <!-- <i class="iconfont icon-zhengyan"></i> -->
                <p class="send_text">发送验证码</p>
              </div>
            </div>
            <div class="login_input">
              <i class="iconfont icon-mima"></i>
              <div class="input_right">
                <input
                :type="pasd ? 'password':'number'"
                  placeholder="请输入密码"
                  class="input_text"
                  v-model="regiserData.password"
                />
                <i class="iconfont " :class="pasd ? 'icon-icon-eye-close':'icon-zhengyan'" @click="selectPasd"></i>
              </div>
            </div>
            <div class="login_input">
              <i class="iconfont icon-xinfeng"></i>
              <div class="input_right">
                <input
                  :type="repPasd ? 'password':'number'"
                  placeholder="确认密码"
                  class="input_text"
                  v-model="regiserData.rep_password"
                />
                <i class="iconfont" :class="repPasd ? 'icon-icon-eye-close':'icon-zhengyan'" @click="selectRepPasd"></i>
              </div>
            </div>
            <div class="login_input">
              <i class="iconfont icon-yaoqingma"></i>
              <input
                type="text"
                placeholder="邀请码"
                class="input_text"
                v-model="regiserData.invitation_code"
              />
            </div>
          </div>
          <div class="login_text">
            <text>Already have an account?</text>
            <text class="text1" @click="toLogin">Go to login </text>
          </div>
          <el-button class="login_btn" @click="handleRegiser" :loading="loading"
            >Register</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useI18n } from "vue-i18n"; // 从 vue-i18n 导入 useI18n
import { ref, onMounted, onUnmounted } from "vue";
import Header from "../../components/Header.vue"; // 引入 Header 组件
import { useRouter } from "vue-router";
import { debounce } from "lodash";
import { register } from "../../api/login.js";
// 使用 Composition API 获取 i18n 实例
const { t, locale } = useI18n();
const router = useRouter();
const regiserData = ref({
  email: "1439835652@qq.com",
  password: "12345678",
  // prive_id:null,
  rep_password: "12345678",
  username: "wangke1",
  verify_code: 1111,
});
const invitation_code = ref("");
const loading = ref(false);
const pasd = ref(true)
const repPasd = ref(true)
onMounted(() => {});

const toLogin = () => {
  router.push("/login");
};
const handleRegiser = debounce(async () => {
  // 邮箱的判断
  const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  const pasReg = /^.{8,25}$/;
  loading.value = true;
  if (!emailRegex.test(regiserData.value.email)) {
    ElMessage.success({
      message: "請輸入正確的郵箱!",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  } else if (!regiserData.value.email) {
    ElMessage.success({
      message: "請輸入郵箱!",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  }

  if (!regiserData.value.verify_code) {
    ElMessage.success({
      message: "請輸入驗證碼!",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  }
  if (!regiserData.value.password) {
    ElMessage.success({
      message: "請輸入密碼!",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  } else if (!pasReg.test(regiserData.value.password)) {
    ElMessage.success({
      message: "请输入密码为8-25个字符!",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  }
  if (!regiserData.value.rep_password) {
    ElMessage.success({
      message: "请输入确认密码!",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  } else if (!pasReg.test(regiserData.value.rep_password)) {
    ElMessage.success({
      message: "请输入确认密码为8-25个字符",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  }
  if (regiserData.value.password !== regiserData.value.rep_password) {
    ElMessage.success({
      message: "新密碼和確認密碼不一致",
      type: "warning",
      plain: true,
    });
    loading.value = false;
    return false;
  }
  const data = { ...regiserData.value };
  
  if (invitation_code.value) {
    data.invitation_code = invitation_code.value;
  }
  const res = await register(data);
  if (res.code == 200) {
    ElMessage.success({
    message: res.message,
    type: 'success',
    plain: true,
  })
    loading.value = false;
    setTimeout(() => {
      router.replace("/");
      localStorage.setItem("token", res.data.Authorization);
      localStorage.setItem("userInfo", res.data.UserInfo);
    }, 1000);
  } else {
    loading.value = false;
  }
});
const selectPasd=()=>{
  pasd.value = !pasd.value
}
const selectRepPasd=()=>{
  repPasd.value = !repPasd.value
}
// 组件卸载时移除滚动监听
onUnmounted(() => {
  // window.removeEventListener("scroll", handleScroll);
});
</script>
<style scoped lang="less">
.login_wrapper {
  width: 100%;
  height: calc(100vh - 60px);
  display: flex;
  align-items: center;
  justify-content: center;

  .contidnex {
    background-color: #18191e;
    min-width: 1200px;
    position: fixed;
    width: 100vw;
    height: 100vh;
    top: 0;
    left: 0;
    right: 0;
    background-image: url("@/assets/login-bg.png");
    background-size: 100%;
  }
  .login_content {
    position: relative;
    z-index: 9;
    width: 1160px;
    height: 640px;
    background-image: url("@/assets/login-form.png");
    background-size: cover;
    display: flex;
    align-items: center;
    justify-content: center;
    .login_left {
      text-align: center;
      width: 530px;
      height: 100%;
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: center;
      .logo_img {
        width: 150px;
        height: 150px;
        border-radius: 10px;
        box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.05);
        margin: 0 auto;
      }
      .text1 {
        padding: 50px 0;
        font-size: 30px;
        color: #fff;
      }
      .text2 {
        color: hsla(0, 0%, 100%, 0.7);
        font-size: 24px;
      }
    }
    .login_right {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      .right_content {
        min-width: 450px;
        color: #fff;
        .page_titlte {
          line-height: 33px;
          font-weight: 700;
          font-size: 24px;
          margin: 4px 0;
        }
        .input_content {
          width: 100%;
          margin-top: 15px;
          .login_input {
            height: 50px;
            width: 100%;
            padding: 0 15px;
            margin-top: 20px;
            border-radius: 5px;
            border: 1px solid #3c3e47;
            font-size: 16px;
            display: flex;
            align-items: center;
            cursor: pointer;
            .icon-mima {
              font-size: 18px;
            }
            .input_text {
              padding: 0 15px;
              height: 100%;
              flex: 1;
              width: 90%;
              font-size: 16px;
              color: #fff;
            }
            .input_right {
              width: 100%;
              height: 100%;
              display: flex;
              align-items: center;
              .input_text {
                padding: 0 15px;
                height: 100%;
                flex: 1;
                width: 100%;
                font-size: 16px;
                color: #fff;
              }
              .send_text {
                padding: 5px 10px;
                font-size: 14px;
                color: #fff;
                background-color: #589cf8;
                border-radius: 5px;
              }
            }
          }
        }
        .login_text {
          width: 100%;
          display: flex;
          align-items: center;
          margin: 20px 0;
          text {
            font-size: 14px;
            cursor: pointer;
          }
          .text1 {
            cursor: pointer;
            color: #589cf8;
            margin-left: 2px;
          }
        }
        .login_btn {
          width: 100%;
          margin-top: 10px;
          background-color: #589cf8;
          color: #fff;
          border-radius: 5px;
          font-size: 16px;
          padding: 2px 0;
          border: none;
          // line-height:2.55;
          height: 45px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
